package utilspackage;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;

/**
 * @author Xiao
 * @date 2021/8/20
 */
public class JDBCUtils {
    /**
     * 定义JDBC工具类JDBCUtils
     */
    private static DataSource dataSource = null;
    static {
        try {
            Properties prop = new Properties();
            //路径写死
            //FileInputStream fis = new FileInputStream("JDBC/src/druid.properties");
            //使用类加载器动态获取src下的资源
            InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties");
            prop.load(is);
            is.close();
            //创建Druid连接池对象
            dataSource = DruidDataSourceFactory.createDataSource(prop);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection() throws Exception {
        return dataSource.getConnection();
    }
}
